package org.lzfto.flink.demo.config.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.ExecutorSubscribableChannel;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@EnableWebSocket
public class WebSocketHandlerConfig implements WebSocketConfigurer {
	private final Logger LOGGER = LoggerFactory.getLogger(getClass());
	/**
	 * 如果你喜欢使用template发送消息，可以定义这么一个bean
	 * @return
	 */
	@Bean
    SimpMessagingTemplate simpMessagingTemplate() {
        return new SimpMessagingTemplate(new ExecutorSubscribableChannel());
    }
	
	@Bean
    ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    	MyWebSocketHandler handler=  new MyWebSocketHandler();
        registry.addHandler(handler, "/websocket")
                .setAllowedOrigins("*");
        LOGGER.info("注册WEBSOCKET处理器");
    }
}
